<?php
namespace Home\Action;
use Think\Page;
class CateAction extends BaseAction {
	public function _initialize() {
        parent::_initialize();
		$reurl=$_SERVER['REQUEST_URI'];
		$reurl=str_replace('index.php/','',$reurl);
		if($this->isMobile()){
		redirect(C('yh_headerm_html').$reurl);	
		}
        $this->_mod = D('items')->cache(true, 5 * 60);
    }
public function index(){
$page	= I('p',1 ,'intval');
$size	= 40;
$cid		= I('cid','','trim');
$sort	= I('sort', 'new', 'trim');
$start = $size * ($page - 1);
$this->assign('txt_sort', $sort);
$this->assign('cid', $cid);
$key    = trimall(I("k",'','htmlspecialchars'));
$key    = urldecode($key);
$where['ems'] = 1;
$where['status'] = 'underway';
if($key){
 $where['title|tags'] = array( 'like', '%' . $key . '%' );
 $this->assign('k', $key);
}
if($cid){
 $where['cate_id'] = $cid;
}
$order = 'is_commend desc,ordid asc';
switch ($sort){
    		case 'new':
				$order.= ', coupon_start_time DESC';
				break;
			case 'price':
				$order.= ', coupon_price ASC';
				break;
			case 'rate':
				$order.= ', quan DESC';
				break;
			case 'hot':
				$order.= ', volume DESC';
				break;
			default:
				$order.= ', '.C('yh_index_sort');
}

$items_list = $this->_mod->where($where)->field('id,pic_url,title,commission_rate,coupon_price,price,quan,shop_type,volume,add_time')->order($order)->limit($start . ',' . $size)->select();	
$count =$this->_mod->where($where)->count();
$pager = new Page($count, $size);
$this->assign('p', $page);
$this->assign('page', $pager->show());
$this->assign('total_item', $count);
$this -> assign('page_size',$size);
if($items_list){
$today=date('Ymd');
$goodslist=array();
foreach($items_list as $k=>$v){
$goodslist[$k]['id']=$v['id'];
$goodslist[$k]['pic_url']=$v['pic_url'];
$goodslist[$k]['title']=$v['title'];
$goodslist[$k]['coupon_price']=number_format($v['coupon_price'], 1);
$goodslist[$k]['commission_rate']=$v['commission_rate'];//比例
$goodslist[$k]['price']=number_format($v['price'], 1);
$goodslist[$k]['quan']=$v['quan'];
$goodslist[$k]['shop_type']=$v['shop_type'];
$goodslist[$k]['volume']=$v['volume'];	
if($today==date('Ymd',$v['add_time'])){
$goodslist[$k]['is_new']=1;	
}else{
$goodslist[$k]['is_new']=0;		
}
if(C('APP_SUB_DOMAIN_DEPLOY')){
$goodslist[$k]['linkurl']=U('/item/',array('id'=>$v['id']));
}else{
$goodslist[$k]['linkurl']=U('item/index',array('id'=>$v['id']));
}
	
}
}
$appkey=trim(C('yh_taobao_appkey'));
$appsecret=trim(C('yh_taobao_appsecret'));
$apppid=trim(C('yh_taobao_pid'));
$apppid=explode('_', $apppid);
$AdzoneId=$apppid[3];
$count=count($items_list);
if(!empty($appkey) && !empty($appsecret) && $key && $count<40 && !empty($AdzoneId)){
vendor("taobao.taobao");
$c = new \TopClient();
$c->appkey = $appkey;
$c->secretKey = $appsecret;
$c->format = 'json';
$req = new \TbkDgMaterialOptionalRequest();
$req->setAdzoneId($AdzoneId);
$req->setPlatform("1");
$req->setPageSize("100");
$req->setSort("tk_total_sales_des");
$req->setHasCoupon("true");
$req->setQ((string)$key);
$req->setPageNo("1");
$resp = $c->execute($req);
$resp = json_decode(json_encode($resp), true);
$resp=$resp['result_list']['map_data'];	
$patterns = "/\d+/";
foreach($resp as $k=>$v){
//preg_match_all($patterns,$v['coupon_info'],$arr);
//$quan=$arr[0];
$goodslist[$k+$count]['quan']=$v['coupon_amount'];
$goodslist[$k+$count]['coupon_click_url']=$v['coupon_share_url'];
$goodslist[$k+$count]['num_iid']=$v['item_id'];
$goodslist[$k+$count]['title']=$v['title'];
$goodslist[$k+$count]['coupon_price']=$v['zk_final_price']-$goodslist[$k+$count]['quan'];
if($v['user_type']=="1"){
$goodslist[$k+$count]['shop_type']='B';	
}else{
$goodslist[$k+$count]['shop_type']='C';	
}
$goodslist[$k+$count]['commission_rate']=$v['commission_rate']; //比例
$goodslist[$k+$count]['price']=$v['zk_final_price'];
$goodslist[$k+$count]['volume']=$v['volume'];
$goodslist[$k+$count]['pic_url']=$v['pict_url'];
}


 
}

$this->assign('list',$goodslist);
if($cid){
$cateinfo=$this->_cate_mod->where('id='.$cid)->field('id,name,seo_title,seo_keys,seo_desc')->find();
}
 if($key){
 $this->_config_seo(array(
            'cate_name' => $cateinfo['name'],
            'title' => '搜索"' . $key . '"的优惠券结果页 - '. C('yh_site_name'),
            'seo_keywords' => $cateinfo['seo_keys'],
            'seo_description' => $cateinfo['seo_desc']
   ));
	
 }else{
 $this->_config_seo(C('yh_seo_config.cate'), array(
            'cate_name' => $cateinfo['name'],
            'seo_title' => $cateinfo['seo_title'],
            'seo_keywords' => $cateinfo['seo_keys'],
            'seo_description' => $cateinfo['seo_desc']
   ));
  }
 
$this->display();

if (preg_match('/[a-zA-Z]/',$key))
{
     return false;
}		
if($goodslist && 12 > strlen($key) && strlen($key)>3){
if(function_exists('opcache_invalidate')){
            $basedir = $_SERVER['DOCUMENT_ROOT']; 
            $dir=$basedir.'/data/Runtime/Data/data/hotkey.php';
            $ret=opcache_invalidate($dir,TRUE);
 }
        $disable_num_iids = F('data/hotkey');
        if(!$disable_num_iids){
            $disable_num_iids = array();
}
 if(count($disable_num_iids)>5){
 $disable_num_iids=array_slice($disable_num_iids,1,5); 
 }
 if(!in_array($key, $disable_num_iids)){
          $disable_num_iids[] = $key;
}
F('data/hotkey',$disable_num_iids);
}


}


	
	
}